package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.DTO.DishDto;
import com.reggie.common.R;
import com.reggie.entity.Category;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.service.CategoryService;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    //    分页效果 与 搜索
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page(page, pageSize);
        Page<DishDto> dishdtoPage = new Page<>();//这是里面的值全是空的
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Dish::getUpdateTime);
        lqw.like(name != null, Dish::getName, name);
        dishService.page(dishPage, lqw);
//        拷贝对象 records列表要另行处理
        BeanUtils.copyProperties(dishPage, dishdtoPage, "records");
//        把records获取出来
        List<Dish> records = dishPage.getRecords();
//        把队列一一遍历出来
        List<DishDto> newDishRecords = records.stream().map((item) -> {
            DishDto dishdtoList = new DishDto();
            BeanUtils.copyProperties(item, dishdtoList);//把item的值赋给dishdtoList

//            根据该次遍历的id来查询分类列表里的 Name
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
//            为了防止有一些id没有对应的分类名称要加一个判断
            if (category != null) {
                String categoryName = category.getName();
                dishdtoList.setCategoryName(categoryName);//此时已把dto里的name赋上值
            }
//            此时用的是dto 有了名字但其他继承dish的属性都为赋值
//            BeanUtils.copyProperties(item, dishdtoList);//把item的值赋给dishdtoList
            return dishdtoList;
        }).collect(Collectors.toList());//把遍历出来的数据 打包收集后转为List格式
//        把新的records放到dishdto的page对象里面去
        dishdtoPage.setRecords(newDishRecords);
//        本来返回的是 dish的现在是dishtopage
        return R.success(dishdtoPage);

    }

    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
//    log.info(String.valueOf(dishDto));
        dishService.setWithFlavor(dishDto);
        return R.success("新增菜品成功");

    }

    //    根据id查菜品信息与口味信息 涉及到两个表
    @GetMapping("/{id}")
    public R<DishDto> editDish(@PathVariable Long id) {
        DishDto dishDto = dishService.getWithFlavor(id);
        return R.success(dishDto);

    }

    @PutMapping
    public R<String> editDishSave(@RequestBody DishDto dishDto) {
//        该功能 把dish与flavor的都save到各自的表中了
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }

    //    停售
    @PostMapping("/status/0")
    public R<String> status(Long ids) {
        Dish dish = dishService.getById(ids);
        dish.setStatus(0);
        dishService.updateById(dish);
        return R.success("已停售");
    }

    //    删除
    @DeleteMapping
    public R<String> remove(Long ids) {
        Dish byId = dishService.getById(ids);
        dishService.removeById(byId);
        return R.success("删除成功");
    }

    //    根据id查找该分类中的菜品列表
    @GetMapping("/list")
    public R<List<DishDto>> dishList(Dish dish) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, dish.getCategoryId());
//        再判断一下该菜品状态
        lambdaQueryWrapper.eq(Dish::getStatus, 1);
//        通过这两个属性排序
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(lambdaQueryWrapper);//获得了所属的菜品信息


        List<DishDto> dishDtoList = dishList.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            //        把dish的信息赋值到dto里面 现在还差口味的未赋值
            BeanUtils.copyProperties(item,dishDto);

            Category category = categoryService.getById(item.getCategoryId());
//            为了防止有一些id没有对应的分类名称要加一个判断
            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);//此时已把dto里的name赋上值
            }
            Long dishId = item.getId();
//我们要在口味名单中通过传过来的菜品id查找到所对应的菜品口味
          LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1= new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> thisFlavors = dishFlavorService.list(lambdaQueryWrapper1);
////            把口味赋值为dto里面
            dishDto.setFlavors(thisFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);

    }

}

